
package cn.hpclub.server.service;

import cn.hpclub.server.constant.Constant;
import cn.hpclub.server.model.MultiUserAuthConfig;
import cn.hpclub.server.util.InterfaceUtil;
import cn.hpclub.server.util.QueryHelper;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.Page;

import java.util.concurrent.ConcurrentHashMap;

/**
 * @FileName: PromotionService.java
 * @Author xsy 
 * @Description:多用户登录Service
 * @CopyRight ZTE Corporation
 */
public class MultiUserAuthConfigService {
    
    public static final int TYPE_CITY = 1;
    public static final int TYPE_STORE = 2;
    public static final int TYPE_MEMBER = 3;
    public static final int TYPE_CHANNEL_CLASS = 4;
    public static final int TYPE_CHANNEL_CLUSTER = 5;
    public static final int TYPE_CHANNEL_TYPE = 6;
    public static final String  KEY_ORG_ID = "orgId";
    public static MultiUserAuthConfigService service = new MultiUserAuthConfigService();


    public Page<Record> getChannel(int type,int pageNumber, int pageSize, ConcurrentHashMap<String, String> map){
        QueryHelper helper = new QueryHelper("select *", Constant.Table.ORGANIZATION_CLASS, "oc");
        helper.addCondition("oc.level=?",type-3);//type-3正好是其层级
        String exact = map.get("queryType");
        helper.addCondition(map.containsKey("keyword") && !"exact".equals(exact), "(oc.class_name like ? or oc.WebsiteId LIKE ?)","%"+map.get("keyword")+"%","%"+map.get("keyword")+"%");
        helper.addCondition(map.containsKey("keyword") && "exact".equals(exact), "(oc.class_name= ? or oc.WebsiteId=?)",map.get("keyword"),map.get("keyword"));
        Page<Record> page = Db.paginate(pageNumber, pageSize, helper.getSelect(), helper.getSqlExceptSelect());
        return page;
    }
    
    public Page<Record> getCity(int pageNumber, int pageSize, ConcurrentHashMap<String, String> map){
        QueryHelper helper = new QueryHelper("SELECT *", Constant.Table.ORGANIZATION,"org");
        helper.addCondition("org.CityCode=org.id ");
        helper.addCondition("org.id in "+InterfaceUtil.getChlidOrgIdSql(InterfaceUtil.getOrgId(),1,true));

        helper.addCondition("org.id not in(select multi_id from multi_user_config WHERE multi_type=1) ");
        String exact = map.get("queryType");
        helper.addCondition(map.containsKey("keyword") && !"exact".equals(exact), "(org.DisplayName LIKE ?)","%"+map.get("keyword")+"%");
        helper.addCondition(map.containsKey("keyword") && "exact".equals(exact), "(org.DisplayName = ?)",map.get("keyword"));
        Page<Record> page = Db.paginate(pageNumber, pageSize, helper.getSelect(), helper.getSqlExceptSelect());
        return page;
    }

    public Page<Record> getStore(int pageNumber, int pageSize, ConcurrentHashMap<String, String> map){
        QueryHelper helper = new QueryHelper("SELECT org.*", Constant.Table.ORGANIZATION,"org");
        /*helper.addCondition("org.parentid=org.AreaCode");*/
        //helper.addCondition("org.parentid>0");
        helper.addCondition(" AreaCode = parentid");
        helper.addCondition(" AreaCode != 0");
        helper.addCondition("org.id in "+InterfaceUtil.getChlidOrgIdSql(InterfaceUtil.getOrgId(),Constant.MAX_ORG_LEVEL,true));
        helper.addCondition("org.is_deleted=0");
        String exact = map.get("queryType");
        helper.addCondition(map.containsKey("keyword") && !"exact".equals(exact), "(org.DisplayName LIKE ?)","%"+map.get("keyword")+"%");
        helper.addCondition(map.containsKey("keyword") && "exact".equals(exact), "(org.DisplayName = ?)",map.get("keyword"));
        Page<Record> page = Db.paginate(pageNumber, pageSize, helper.getSelect(), helper.getSqlExceptSelect());
        return page;
    }

    public Page<Record> getMember(int pageNumber, int pageSize, ConcurrentHashMap<String, String> map){
        QueryHelper helper = new QueryHelper("select mo.id,mo.name,mo.emp_code,mo.mobile", Constant.Table.MEMBER_ORG, "mo");
        String orgFilter = InterfaceUtil.getChlidOrgIdSql(InterfaceUtil.getOrgId(), Constant.MAX_ORG_LEVEL, true);
        helper.addCondition("mo.org_id IN"+orgFilter);
        helper.addCondition("mo.enabled=1");
        helper.addCondition("mo.is_deleted=0");
        helper.addCondition(map.containsKey("keyword"), "(mo.name like ? or mo.user_name =? or mo.mobile=?)","%"+map.get("keyword")+"%",map.get("keyword"),map.get("keyword"));
        Page<Record> page = Db.paginate(pageNumber, pageSize, helper.getSelect(), helper.getSqlExceptSelect());
        return page;
    }
    
    public Page<Record> queryChannelType(int pageNumber, int pageSize, ConcurrentHashMap<String, String> map){
        QueryHelper helper = new QueryHelper("select *", Constant.Table.ORGANIZATION_CLASS, "oc");
        helper.addCondition("oc.id IN(SELECT multi_id FROM multi_user_config WHERE multi_type=? AND creator_org_id=?)",MultiUserAuthConfigService.TYPE_CHANNEL_TYPE,map.get(KEY_ORG_ID));
        //helper.addCondition(map.containsKey("channelType"), "oc.class_name like ? or oc.WebsiteId=?",map.get("channelType"),map.get("channelType"));
        helper.addCondition(map.containsKey("keywordChannelType"), "(oc.class_name LIKE ? OR oc.WebsiteId=?)","%"+map.get("keywordChannelType")+"%",map.get("keywordChannelType"));
        Page<Record> page = Db.paginate(pageNumber, pageSize, helper.getSelect(), helper.getSqlExceptSelect());
        return page;
    }
    public Page<Record> queryChannelCluster(int pageNumber, int pageSize, ConcurrentHashMap<String, String> map){
        QueryHelper helper = new QueryHelper("select *", Constant.Table.ORGANIZATION_CLASS, "oc");
        helper.addCondition("oc.id IN(SELECT multi_id FROM multi_user_config WHERE multi_type=? AND creator_org_id=?)",MultiUserAuthConfigService.TYPE_CHANNEL_CLUSTER,map.get(KEY_ORG_ID));
        helper.addCondition(map.containsKey("keywordChannelCluster"), "(oc.class_name LIKE ? OR oc.WebsiteId=?)","%"+map.get("keywordChannelCluster")+"%",map.get("keywordChannelCluster"));
        Page<Record> page = Db.paginate(pageNumber, pageSize, helper.getSelect(), helper.getSqlExceptSelect());
        return page;
    }
    public Page<Record> queryChannelClass(int pageNumber, int pageSize, ConcurrentHashMap<String, String> map){
        QueryHelper helper = new QueryHelper("select *", Constant.Table.ORGANIZATION_CLASS, "oc");
        helper.addCondition("oc.id IN(SELECT multi_id FROM multi_user_config WHERE multi_type=? AND creator_org_id=?)",MultiUserAuthConfigService.TYPE_CHANNEL_CLASS,map.get(KEY_ORG_ID));
        helper.addCondition(map.containsKey("keywordChannelClass"), "(oc.class_name LIKE ? OR oc.WebsiteId=?)","%"+map.get("keywordChannelClass")+"%",map.get("keywordChannelClass"));
        Page<Record> page = Db.paginate(pageNumber, pageSize, helper.getSelect(), helper.getSqlExceptSelect());
        return page;
    }
    public Page<Record> queryMember(int pageNumber, int pageSize, ConcurrentHashMap<String, String> map){
        QueryHelper helper = new QueryHelper("select *", Constant.Table.MEMBER_ORG, "mo");
        helper.addCondition("mo.id IN(SELECT multi_id FROM multi_user_config WHERE multi_type=? AND creator_org_id=?)",MultiUserAuthConfigService.TYPE_MEMBER,map.get(KEY_ORG_ID));
        helper.addCondition(map.containsKey("keywordMember"), "(mo.name LIKE ? OR mo.emp_code=? OR mo.mobile=?)","%"+map.get("keywordMember")+"%",map.get("keywordMember"),map.get("keywordMember"));
        Page<Record> page = Db.paginate(pageNumber, pageSize, helper.getSelect(), helper.getSqlExceptSelect());
        return page;
    }
    public Page<Record> queryStore(int pageNumber, int pageSize, ConcurrentHashMap<String, String> map){
        QueryHelper helper = new QueryHelper("select *", Constant.Table.ORGANIZATION, "org");
        helper.addCondition("org.id IN(SELECT multi_id FROM multi_user_config WHERE multi_type=? AND creator_org_id=?)",MultiUserAuthConfigService.TYPE_STORE,map.get(KEY_ORG_ID));
        helper.addCondition(map.containsKey("keywordOrg"), "(org.DisplayName LIKE ? OR org.NumCode=?)","%"+map.get("keywordOrg")+"%",map.get("keywordOrg"));
        Page<Record> page = Db.paginate(pageNumber, pageSize, helper.getSelect(), helper.getSqlExceptSelect());
        return page;
    }
    public Page<Record> queryCity(int pageNumber, int pageSize, ConcurrentHashMap<String, String> map){
        QueryHelper helper = new QueryHelper("select *", Constant.Table.ORGANIZATION, "org");
        helper.addCondition(map.containsKey("keywordCity"), "(org.DisplayName LIKE ? OR org.NumCode=?)","%"+map.get("keywordCity")+"%",map.get("keywordCity"));
        helper.addCondition("org.id IN(SELECT multi_id FROM multi_user_config WHERE multi_type=? AND creator_org_id=?)",MultiUserAuthConfigService.TYPE_CITY,map.get(KEY_ORG_ID));
        Page<Record> page = Db.paginate(pageNumber, pageSize, helper.getSelect(), helper.getSqlExceptSelect());
        return page;
    }
    /**
     * 删除表数据
     * @param type
     * @param flow_id
     * @param ids
     * @return
     */
    public boolean deleteByType(int type,Integer[] ids){
        StringBuffer sb = new StringBuffer();
        int org_id = InterfaceUtil.getOrgId();
        
        sb.append("DELETE FROM " + Constant.Table.MULTI_USER_CONFIG + " WHERE "+MultiUserAuthConfig.MULTI_TYPE+"="+type+" AND creator_org_id="+org_id+" AND "+MultiUserAuthConfig.MULTI_ID+" IN(");
        if(ids.length > 0){
            for(int i = 0; i < ids.length; i++){
                if(i == ids.length - 1){
                    sb.append("?");
                } else{
                    sb.append("?,");
                }
            }
            sb.append(")");
        } else{
            return false;
        }
        return Db.update(sb.toString(), (Object[])ids) > 0;
    }
    /**
     * 保存
     * @param type
     * @param flow_id
     * @param ids
     * @return
     */
    public boolean saveByType(int type, Integer[] ids){
        StringBuffer sb = new StringBuffer();
        int org_id = InterfaceUtil.getOrgId();
        sb.append("INSERT INTO " + Constant.Table.MULTI_USER_CONFIG + " (" +MultiUserAuthConfig.MULTI_TYPE+","+MultiUserAuthConfig.MULTI_ID+","+MultiUserAuthConfig.CREATOR_ORG_ID+") VALUES(?,?,?)");
        for(int id:ids ){
            //查询数据是否已存在，存在就直接跳过
            String selectExistData = "select * from "+Constant.Table.MULTI_USER_CONFIG+" WHERE multi_type=? AND multi_id=? AND creator_org_id=?";
            Record record = Db.findFirst(selectExistData,type,id,org_id);
            if(record!=null && record.getInt("id")>0){
                continue;
            }
            Db.update(sb.toString(),type,id,org_id);
        }
        return true;
    }

    /**
     * 删除某一类配置的全部数据
     * @param type
     * @return
     */
    public int deleteAllByType(int type){
        int org_id = InterfaceUtil.getOrgId();
        String deleteSql = "delete from multi_user_config where multi_type=? AND creator_org_id=?";
        return Db.update(deleteSql,type,org_id);
    }

}
